package com.central.apps.handler.project;

import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.app.service.TreeService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 处理项目台账业务逻辑
 *
 * @author lidachao
 * @date 2023/5/16
 */
@Component
@Slf4j
public class ProjectAccountHandler extends BaseHandler {

    @Autowired
    private HandlerService handlerService;
    @Autowired
    private TreeService treeService;

    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        if ("getCostFill".equals(type)) {
            Integer id = MapUtils.getInteger(params, "id");
            Integer pbiId = MapUtils.getInteger(params, "pbiid");
            Integer proId = MapUtils.getInteger(params, "proid");

            List<Map<String, Object>> list = handlerService.findBySql("select id from gq_probudget where proid = " + proId + " and pbiid = " + pbiId);

            if (CollectionUtils.isNotEmpty(list)) {
                combineChildData(list, proId, pbiId);
            }

            StringBuilder sb = new StringBuilder();
            for (Map<String, Object> map : list) {
                sb.append(MapUtils.getInteger(map, "id")).append(",");
            }

            sb.deleteCharAt(sb.length() - 1);

            list = handlerService.findBySql("select id, cfnum, cfdesc, type, costsource, totalcost, dutycenter, createuserdesc, DATE_FORMAT(createtime, '%Y-%m-%d %H:%i:%s') AS createtime from gq_procostfill where  status in ('已批准','已生成','已分配','未分配') and proid = " + proId + " and pbdid in (" + sb + ") ");

            if (CollectionUtils.isNotEmpty(list)) {
                JSONObject json = new JSONObject();
                json.put("code", "0");
                json.put("count", list.size());
                json.put("data", list);
                return json;
            }
        }
        if ("getDeliDetail".equals(type)) {
            Integer taskId = MapUtils.getInteger(params, "taskid");
            Integer proId = MapUtils.getInteger(params, "proid");

            // 查询我的任务信息
            Map<String, Object> treeParams = new HashMap<>(4);
            treeParams.put("object", "gq_scheduleplantask");
            treeParams.put("tree_id", "id");
            treeParams.put("tree_pid", "parentId");
            treeParams.put("proid", proId);
            treeParams.put("parent_filter", "id=" + taskId);
            List<Map<String, Object>> list = treeService.findTreeData("proplanApp", treeParams);

            StringBuilder sb = new StringBuilder();
            for (Map<String, Object> map : list) {
                sb.append(MapUtils.getInteger(map, "id")).append(",");
            }
            sb.deleteCharAt(sb.length() - 1);

            list = handlerService.findBySql("select id, pdid, createuserdesc, DATE_FORMAT(createtime, '%Y-%m-%d %H:%i:%s') AS createtime, docversion, " +
                    " fileid, taskdelid, delcategory, taskdelname, deltype from gq_prodocfile where id in (select max(id) as id from gq_prodocfile where proid = " + proId
                    + " and pdid in (select id from gq_prodocument where proid = " + proId
                    + " and taskid in (" + sb.toString() + ") and taskdelid in (select id from gq_taskdeliverables where taskid in (" + sb.toString() + "))) group by taskid, taskdelid)");

            if (CollectionUtils.isNotEmpty(list)) {
                JSONObject json = new JSONObject();
                json.put("code", "0");
                json.put("count", list.size());
                json.put("data", list);
                return json;
            }
        }
        return null;
    }

    /**
     * 组织子级预算
     *
     * @param list
     * @param proId
     * @param pbiId
     */
    private void combineChildData(List<Map<String, Object>> list, Integer proId, Integer pbiId) {
        List<Map<String, Object>> tempList = handlerService.findBySql("select id, pbiid from gq_probudget where proid = " + proId + " and pbiparentid = " + pbiId);

        if (CollectionUtils.isNotEmpty(tempList)) {
            List<Integer> pbiIdList = new ArrayList<>();
            for (Map<String, Object> tempMap : tempList) {
                list.add(tempMap);

                Integer tempPbiId = MapUtils.getInteger(tempMap, "pbiid");
                if (tempPbiId > 0 && !pbiIdList.contains(tempPbiId)) {
                    pbiIdList.add(tempPbiId);
                }
            }

            if (CollectionUtils.isNotEmpty(pbiIdList)) {
                for (Integer tempPbiId : pbiIdList) {
                    combineChildData(list, proId, tempPbiId);
                }
            }
        }
    }

}
